package com.gui.spring.ioc;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

public class FactoryBeanDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        System.out.println("========================================");
        Arrays.stream(context.getBeanDefinitionNames()).filter(item -> !item.startsWith("org.springframework")).forEach(System.out::println);

        Object colorFactoryBean = context.getBean("colorFactoryBean");
        System.out.println(colorFactoryBean.getClass().getName());

        System.out.println("========================================");
        ColorFactoryBean factoryBean = context.getBean(ColorFactoryBean.class);
        System.out.println(factoryBean.getClass());
    }


    static class Color {

    }

    static class ColorFactoryBean implements FactoryBean<Color> {

        @Override
        public Color getObject() throws Exception {
            System.out.println("ColorFactoryBean getObject....");
            return new Color();
        }

        @Override
        public Class<?> getObjectType() {
            return Color.class;
        }
    }

    @Configuration
    static class Config {

        @Bean
        public ColorFactoryBean colorFactoryBean(){
            return new ColorFactoryBean();
        }

    }
}
